package fragment.listfragments;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.baoyz.widget.PullRefreshLayout;
import com.lanou.horo.myapplication.R;

import java.util.List;

import entity.ListAnchorEntity;
import fragment.AbsBaseFragment;
import fragment.tabhostFragment.PersonalDetailsFragment;
import tools.ItemOnClickListener;
import utils.adapter.ListAnchorAdpter;
import volley.VolleySingleton;

/**
 * Created by dllo on 15/11/24.
 */
public class ListAnchorFragment extends AbsBaseFragment implements ItemOnClickListener {
    private RecyclerView recyclerView;
    private ListAnchorAdpter adpter;
    private TextView title;
    String titleName,URL;
    private ImageView back;
    PullRefreshLayout layout;


    @Override
    protected int getLayoutView() {
        return R.layout.fragment_anchor_list;
    }

    @Override
    protected void initView() {
        recyclerView = findView(R.id.listAnchor);
        GridLayoutManager manager = new GridLayoutManager(getContext(),1);
        recyclerView.setLayoutManager(manager);

        back = findView(R.id.titleForAllBack);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goBack();
            }
        });
        title = findView(R.id.titleForAllTv);
        layout = findView(R.id.swipeRefreshLayoutlist);

    }

    //静态方法传值
    public static ListAnchorFragment newInstance(String URL,String titleName) {
        ListAnchorFragment fragment = new ListAnchorFragment();
        Bundle args = new Bundle();
        args.putString("URL", URL);
        args.putString("title", titleName);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void initData() {
        adpter = new ListAnchorAdpter();
        recyclerView.setAdapter(adpter);
        adpter.setItemListener(this);
        layout.setOnRefreshListener(new PullRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                layout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        layout.setRefreshing(false);
                    }
                },400);
            }
        });

        if (getArguments() != null) {
            URL = getArguments().getString("URL");
            titleName = getArguments().getString("title");
            title.setText(titleName);
            initNetwork();

        }



    }
    private void initNetwork() {
        VolleySingleton.getInstance().addRequest(URL,
                ListAnchorEntity.class, new Response.Listener<ListAnchorEntity>() {
                    @Override
                    public void onResponse(ListAnchorEntity response) {
                        if (response != null) {
                            List<ListAnchorEntity.ListEntity> datas = response.getList();
                            if (datas != null) {
                                adpter.addData(datas);
                            }
                            dismissDialog();
                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        dismissDialog();
                        toastError();
                    }
                });
        showDialog();
    }

    @Override
    public void itemListener(Object o) {
        ListAnchorEntity.ListEntity entity = (ListAnchorEntity.ListEntity) o;
        goToNext(new PersonalDetailsFragment(entity.getUid()));
    }
}
